﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using CommonLibrary.DataModel.WorkFlow;
using CommonLibrary.DB;
using System.Data;

namespace ServiceApplication.Dao.WorkFlow
{
    public class WfSpqxDao
    {
        public int AddEntity(WfSpqxEntity wfSpqxEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@spqx_id", wfSpqxEntity.spqx_id));
            parameterList.Add(new SqlParameter("@spqx_name", wfSpqxEntity.spqx_name));
            parameterList.Add(new SqlParameter("@spqx_kybz", wfSpqxEntity.spqx_kybz));

            string sql = "insert into wf_spqx (spqx_name,spqx_kybz) values " +
                " (@spqx_name,@spqx_kybz); " +
                " select @@identity";
            string id = DBExecuteUtil.getUniqueResult(sql, parameterList.ToArray());
            return int.Parse(id);
        }

        public void DeleteEntity(int spqx_id)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@spqx_id", spqx_id));
            string sql = "delete from wf_spqx where spqx_id=@spqx_id ";
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        public void UpdateEntity(WfSpqxEntity wfSpqxEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@spqx_id", wfSpqxEntity.spqx_id));
            parameterList.Add(new SqlParameter("@spqx_name", wfSpqxEntity.spqx_name));
            parameterList.Add(new SqlParameter("@spqx_kybz", wfSpqxEntity.spqx_kybz));

            string sql = "update wf_spqx set spqx_name=@spqx_name,spqx_kybz=@spqx_kybz where spqx_id=@spqx_id ";

            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        public WfSpqxEntity QueryEntity(int spqx_id)
        {
            WfSpqxEntity wfSpqxEntity = null;
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@spqx_id", spqx_id));
            string sql = "select spqx_id,spqx_name,spqx_kybz  from wf_spqx where spqx_id=@spqx_id";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (result == null) return null;

            wfSpqxEntity = new WfSpqxEntity();
            wfSpqxEntity.spqx_id = int.Parse(result[0][0]);
            wfSpqxEntity.spqx_name = result[0][1];
            wfSpqxEntity.spqx_kybz = int.Parse(result[0][2]);
            return wfSpqxEntity;
        }

        public DataTable QueryEntity(string[] spqx_names)
        {
            if (spqx_names == null || spqx_names.Length == 0) return null;

            string sql = "select spqx_id 编码,spqx_name 名称,spqx_kybz 可用标志 from wf_spqx  where spqx_name in (";
            foreach (var item in spqx_names)
            {
                sql += "'" + item + "',";
            }
            sql = sql.Substring(0, sql.Length - 1);
            sql += ")";
            var result = DBExecuteUtil.querySqlTable(sql);
            return result;
        }


        public DataTable QueryAll(int spqx_kybz)
        {
            string sql = "select spqx_id 编码,spqx_name 名称,spqx_kybz 可用标志 from wf_spqx where spqx_id=spqx_id ";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            if (spqx_kybz == 1 || spqx_kybz == 0)
            {
                parameterList.Add(new SqlParameter("@spqx_kybz", spqx_kybz));
                sql += " and spqx_kybz=@spqx_kybz ";
            }
            DataTable dt = DBExecuteUtil.querySqlTable(sql,parameterList.ToArray());
            return dt;
        }
    }
}
